<?php
Class Paper extends Model
{
	public $id;
	public $description;
	public $content;
	public $questions;
	public $paperbasicid;
	
	public function __construct()
	{
		parent::Model();
		$this->id=-1;
		$this->paperbasicid=-1;
	}
	
	public function DeleteByID($id)
	{
		$this->db->trans_start();
		try 
		{
			$sql="Delete from questions where PaperID=$id";
			$this->db->query($sql);
			$sql="delete from papers where ID=$id";
			$this->db->query($sql);	
		} catch (Exception $e) 
		{
			$this->db->trans_complete();
			return 0;
		}
		
		$this->db->trans_complete();
		return 1;
	}
	
	public function Search($param)
	{
		if ($param->ID!=-1)
		{
			$id=$param->ID;
			$sql="select ID,Description,CreationDate from papers where ID=$id";
		}
		else 
		{
			$where="";
			if ($param->DateFrom!='' || $param->DateTo!='')
			{
				$from=$param->DateFrom;
				$to=$param->DateTo;
				if ($from!='') $where=$where."(CreationDate>=$from and ";
				if ($to!='') $where=$where."(CreationDate<=$to and ";
			}	
			else 
				return 0;	
				
			$sql="select ID,Description,PaperHeader,CreationDate from papers ".$where."1=1";
		}
		
		try {
			$query=$this->db->query($sql);
			foreach ($query->result() as $row)
			{
				$res["ID"]=$row->ID;
				$res["Description"]=$row->Description;
				$res["CreationDate"]=$row->CreationDate;
				//display
			}
		} catch (Exception $e) {
			return 0;
		}

		return 1;
	}
	
	public function Add()
	{
		$data=array(
		'Description'=>$this->description,
		'Content'=>$this->content,
		'CreationDate'=>);
		
		try {
			if ($this->id!=-1)
			{
				$this->db->insert_string('papers',$data);
				$this->id=$this->db->insert_id();
			}
			else
			{
				$id=$this->id;
				$this->db->update_string('papers',$data,"ID=$id");
			}
			
			foreach ($this->questions as $que)
			{
				if ($que->IsNew && $que->HasModifed) throw new Exception("Invalid Parameters!");
				
				$data=array(
				'Content'=>$que->Content,
				'Answer'=>$que->Answer,
				'Score'=>$que->Score,
				'CreationDate'=>,
				'PaperID'=>$this->id);
				if ($que->IsNew)
				{
					$this->db->insert_string('questions',$data);
					$que->id=$this->db-insert_id();
					continue;
				}
				
				if ($que->HasModifed)
				{
					$id=$que->ID;
					$this->db->update_string('questions',$data,"ID=$id");
					continue;
				}
			}
		} catch (Exception $e) {
			return 0;
		}
		
		return 1;
	}
	
	public function Modify()
	{
		//read
		//get
		//modify
		
		$this->Add();
	}
	
	private function Get()
	{
		//read id into $id
		$sql="select ID,Content,CreationDate,PaperBaiscID,Description form papsers where ID=$id";
		try {
			$query=$this->db->query($sql);
			$pap=$query->row();
			$this->id=$pap->ID;
			$this->content=$pap->Content;
			$this->description=$pap->Description;
			$this->paperbasicid=$pap->PaperBasicID;
			
			$id=$this->id;
			$sql="select ID,Content,Answer,Score,CreationDate from questions where PaperID=$id Order By ID";
			$query=$this->db->query($sql);
			foreach ($query->result() as $row)
			{
				$que=new Question();
				$que->ID=$row->ID;
				$que->Content=$row->Content;
				$que->Answer=$row->Answer;
				$que->Score=$row->Score;
				array_push($this->questions,$que);
			}
		} catch (Exception $e) {
		}
	}
	
	public function Generate($papermodel)
	{
		//save to to session
		// except the details of each questions content has included all of information,
		//such as header,tasks,question id, and all of these information has been formated
		$this->Add();
	}
	
	public function Display($ForModify)
	{
		
	}
}

Class Question
{
	public $ID;
	public $Content;
	public $Answer;
	public $Score;
	public $IsNew;
	public $HasModified;
	
	public function __construct()
	{
		$this->ID=-1;
		$this->Score=-1;
		$this->IsNew=false;
		$this->HasModified=false;
	}
	
	public function FuzzyGeneration($CategoryID,$TaskID,$Difficulty,$Scores)
	{
		
	}
	
	public function ExactGeneration($QuestionID,$ContentIndex=-1,$AnswerIndex=-1)
	{
		
	}
}
?>